//
//  Request.m
//  DesignPattern
//
//  Created by jun on 2021/4/16.
//

#import "Request.h"

@implementation Request

+ (NSURLRequest *)netGetURL:(NSString *)url params:(NSDictionary *)params compelete:(ClientCompelete)compelete{
    if (compelete) {
        compelete(YES,@{@"data":@{@"title":@"title",@"desc":@"desc"}},@"成功",nil);
    }
    return [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
}

+(NSURLRequest *)netPostURL:(NSString *)url params:(NSDictionary *)params bodyParams:(NSDictionary *)params compelete:(ClientCompelete)compelete{
    return [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
}
+(void)handelSuccesss:(ClientCompelete)compelete url:(NSString*)url responseObject:(id)responseObject{
//    BNIMDataLog(@"短链接结束请求:\n url:%@%@ \n responseObject:%@",[BCHttpRequestCenter defaultCenter].baseURL,url,responseObject);
    if ([self isRefreshToken:responseObject]) {
        
    }else if ([self isLogOut:responseObject]){
//        [[IMCentre Share] postNotice:NOTICE_IM_LOGOUT Tip:[self getMessage:responseObject]];
    }
    if (compelete) {
        compelete([self isSuccess:responseObject],responseObject,[self getMessage:responseObject],nil);
    }
}
+(void)handleFail:(ClientCompelete)compelete url:(NSString*)url error:(NSError *)error{
//    BNIMDataLog(@"短链接结束请求:\n url:%@%@ \n error.localizedDescription:%@",[BCHttpRequestCenter defaultCenter].baseURL,url,error.localizedDescription);
    if (compelete) {
        compelete(NO,nil,error.localizedDescription,error);
    }
}

+(BOOL)isSuccess:(NSDictionary *)dic{
    return [dic isKindOfClass:[NSDictionary class]]&&[dic[@"code"] integerValue]==10200&&[dic[@"isSuccess"] boolValue];
}
+(NSString *)getMessage:(NSDictionary *)dic{
    NSString *msg = nil;
    if ([dic isKindOfClass:[NSDictionary class]]) {
        msg = dic[@"message"];
    }
    if(!msg){
        msg = @"未知错误";
    }
    return msg;
}
+(BOOL)isRefreshToken:(NSDictionary *)dic{
    return [dic isKindOfClass:[NSDictionary class]]&&([dic[@"code"] integerValue]==-2);
//    return [dic isKindOfClass:[NSDictionary class]]&&([dic[@"code"] integerValue]==BCHttpRequestStateTokenInvalid);
}
+(BOOL)isLogOut:(NSDictionary *)dic{
    return [dic isKindOfClass:[NSDictionary class]]&&([dic[@"code"] integerValue]==-11);
//    return [dic isKindOfClass:[NSDictionary class]]&&([dic[@"code"] integerValue]==BCHttpRequestStateRefreshTokenFull||[dic[@"code"] integerValue]==BCHttpRequestStateNoRefreshToken||[dic[@"code"] integerValue]==BCHttpRequestStateRefreshTokenInvalid||[dic[@"code"] integerValue]==BCHttpRequestStateOtherLogin);
}
@end
